﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.Structure;
using TagPropagator.Enums;
namespace TagPropagator
{
    public class FeatureExtractor : IFeatureExtracor
    {
        public SerializableDictionary<string, Feature> ExtractFeatures(System.Drawing.Image image)
        {
            SerializableDictionary<string, Feature> ret = new SerializableDictionary<string,Feature>();
            Feature h0 = new Feature(), h1 = new Feature(), h2 = new Feature();
            ret.Add("histogram0", h0);
            ret.Add("histogram1", h1);
            ret.Add("histogram2", h2);
            using (System.Drawing.Bitmap bm = new System.Drawing.Bitmap(image))
            {
                Image<Bgr, byte> img = new Image<Bgr, byte>(bm);
                Image<Gray, Byte>[] channels = img.Split();
                DenseHistogram histogram0 = new DenseHistogram(20, new RangeF(0, 255));
                CvInvoke.cvCalcHist(new System.IntPtr[] { channels[0].Ptr }, histogram0, false, IntPtr.Zero);
                h0.Data = histogram0;
                h0.Type = FeatureType.HISTOGRAM;
                DenseHistogram histogram1 = new DenseHistogram(20, new RangeF(0, 255));
                CvInvoke.cvCalcHist(new System.IntPtr[] { channels[1].Ptr }, histogram1, false, IntPtr.Zero);
                h1.Data = histogram1;
                h1.Type = FeatureType.HISTOGRAM;
                DenseHistogram histogram2 = new DenseHistogram(20, new RangeF(0, 255));
                CvInvoke.cvCalcHist(new System.IntPtr[] { channels[2].Ptr }, histogram2, false, IntPtr.Zero);
                h2.Data = histogram2;
                h2.Type = FeatureType.HISTOGRAM;
            }
            return ret;
        }
    }
}
